package com.du.javaweb.servlet; /**
 * @author : DuJiabao
 * @Date : 2021/3/28 23:38
 * @Project : java-web
 * @File : ${FILE_NAME}
 * @Desc :
 */

import com.du.javaweb.pojo.Person;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AjaxServlet", value = "/AjaxServlet")
public class AjaxServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("接收到了请求");
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=utf-8");

    Person person = new Person(1, "jack");
    String toJson = new Gson().toJson(person);
    response.getWriter().write(toJson);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }
}
